package com.example.final_project.Home.frag;

import android.content.Context;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.ejlchina.okhttps.OkHttps;
import com.example.final_project.Home.adapter.TimeInfoAdapter;
import com.example.final_project.Home.entry.TimeInfo;
import com.example.final_project.R;
import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.ClassicsHeader;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import java.util.List;


public class PlayCarFragment extends Fragment {

    private Context context;
    private RecyclerView recyclerView;
    private int pageNow=1;
    private int pageSize=4;
    public static final int INIT=0;
    public static final int LOADING=1;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_play_car, container, false);
        context = getContext();
        recyclerView = view.findViewById(R.id.home_info);
        RefreshLayout refreshLayout = view.findViewById(R.id.refreshLayout);
        refreshLayout.setRefreshHeader(new ClassicsHeader(context));
        refreshLayout.setRefreshFooter(new ClassicsFooter(context));
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                refreshLayout.finishRefresh(2000/*,false*/);//传入false表示刷新失败
            }
        });
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshlayout) {
                if(pageNow < 3){
                    pageNow++;
                }else{
                    pageNow=1;
                }
                LoadData();
                refreshlayout.finishLoadMore(true);//传入false表示加载失败
            }
        });
        getData();
        return view;
    }

    private Handler handler = new Handler(Looper.getMainLooper()){

        private TimeInfoAdapter timeInfoAdapter;

        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case INIT:
                    List<TimeInfo> infos = (List<TimeInfo>)msg.obj;
                    timeInfoAdapter = new TimeInfoAdapter(context,infos);
                    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context,RecyclerView.VERTICAL,false);
                    recyclerView.setAdapter(timeInfoAdapter);
                    recyclerView.setLayoutManager(linearLayoutManager);
                    break;
                case LOADING:
                    List<TimeInfo> list = (List<TimeInfo>)msg.obj;
                    timeInfoAdapter.getData(list);
                    break;
            }
        }
    };

    public void getData(){
        OkHttps.async("http://10.150.11.9:8086/chengcar/findInfoByPage/")
                .bind(context)
                .addUrlPara("pageNow",pageNow)
                .addUrlPara("pageSize",pageSize)
                .setOnResList(TimeInfo.class,infos ->{
                    Message message = Message.obtain();
                    message.what=INIT;
                    message.obj = infos;
                    handler.sendMessage(message);
                })
                .get();
    }

    public void LoadData(){
        OkHttps.async("http://10.150.11.9:8086/chengcar/findInfoByPage/")
                .bind(context)
                .addUrlPara("pageNow",pageNow)
                .addUrlPara("pageSize",pageSize)
                .setOnResList(TimeInfo.class,infos ->{
                    Message message = Message.obtain();
                    message.what=LOADING;
                    message.obj = infos;
                    handler.sendMessage(message);
                })
                .get();
    }

}